package com.api.weavernorth.retailExperienceAward;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;


/**
 * @Author: bruseewang
 * @Package: com.api.weavernorth.retailExperienceAward
 * @date: 2020/6/10 11:15
 * @description:
 */
@Path("/weavernorth/retailExperienceAward")
public class retailExperienceSource extends HttpServlet {
//    private static Log log = LogFactory.getLog(retailExperienceSource.class);
//private Logger log = Logger4jUtil.getLoggerByName("/action/api/retailExperienceSource/");
    /***
     * 获取店铺的平均分
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDppjf")
    @Produces(MediaType.TEXT_PLAIN)
    public String dppjf(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Logger log = Logger4jUtil.getLoggerByName("/action/api/retailExperienceSource/");
        log.info("获取店铺平均分接口开始=======>");
        RecordSet rs_zf=new RecordSet();
        //返回信息
        JSONArray jsonArray = new JSONArray();
        String strKhzqNow=Util.null2String(request.getParameter("khzq"));
        log.info("当前考核周期："+strKhzqNow);
        String strKhzqPrev= dateUtils.previousMonth(strKhzqNow);
        log.info("上个考核周期："+strKhzqPrev);
        //根据考核周期,从终端店铺分数统计表中取出各项评分的平均分
        String strGetFs_avg="SELECT khzq, ROUND(AVG(fwbz40), 2) as fwbz_avg,ROUND(AVG(clbz20), 2) as clbz_avg,ROUND(AVG(yybz35), 2) as yybz_avg,ROUND(AVG(zdf), 2) as zdf_avg  FROM uf_zddpfstj where dpjb in (0,1) and khzq='"+strKhzqPrev+"' GROUP BY khzq";
        String strFwbz_avg=null;//服务标准平均分
        String strClbz_avg=null;//陈列标准平均分
        String strYybz_avg=null;//营运标准平均分
        String strZdf_avg=null;//总得分平均分
        log.info("获取店铺评分的各项指标平均分SQL:"+strGetFs_avg);
        rs_zf.executeQuery(strGetFs_avg);
        try {
            while (rs_zf.next()){
                JSONObject jsonObject=new JSONObject();
                strFwbz_avg= Util.null2String(rs_zf.getString("fwbz_avg"));
                strClbz_avg= Util.null2String(rs_zf.getString("clbz_avg"));
                strYybz_avg= Util.null2String(rs_zf.getString("yybz_avg"));
                strZdf_avg= Util.null2String(rs_zf.getString("zdf_avg"));
                jsonObject.put("fwbz_avg",strFwbz_avg);
                jsonObject.put("clbz_avg",strClbz_avg);
                jsonObject.put("yybz_avg",strYybz_avg);
                jsonObject.put("zdf_avg",strZdf_avg);
                jsonArray.add(jsonObject);
            }
        }catch (Exception ex){
            log.info("获取平均分出现异常,异常信息为："+ex.toString());
        }
        log.info("考核周期:"+strKhzqPrev+";服务标准平均分："+strFwbz_avg+";陈列标准平均分："+strClbz_avg+";营运标准平均分："+strYybz_avg+";总分平均分："+strZdf_avg);
        return jsonArray.toString();
    }
}
